package net.isocron.jiminy;

import net.isocron.jiminy.api.Statusnet;
import net.isocron.jiminy.api.StatusnetException;
import net.isocron.jiminy.db.AccountsDbAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Login extends Activity {
    public ProgressDialog progressDialog;
    private EditText UserEditText;
    private EditText PassEditText;
    AccountsDbAdapter mDbHelper;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        mDbHelper = new AccountsDbAdapter(this);
        mDbHelper.open();
        
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Please wait...");
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(false);
       
        UserEditText = (EditText) findViewById(R.id.username);
        PassEditText = (EditText) findViewById(R.id.password);
       
        Button button = (Button) findViewById(R.id.okbutton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int usersize = UserEditText.getText().length();
                int passsize = PassEditText.getText().length();
                if(usersize > 0 && passsize > 0) {
                    progressDialog.show();
                    String user = UserEditText.getText().toString();
                    String pass = PassEditText.getText().toString();
                    doLogin(user, pass);
                } else createDialog("Error","Please enter Username and Password");
            }
        });
       
        button = (Button) findViewById(R.id.cancelbutton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) { quit(false, null); }
        });
    }

    private void quit(boolean success, Intent i) {
        setResult( (success) ? -1:0, i);
        finish();
    }
   
    private void createDialog(String title, String text) {
        AlertDialog ad = new AlertDialog.Builder(this)
        .setPositiveButton("Ok", null)
        .setTitle(title)
        .setMessage(text)
        .create();
        ad.show();
    }
   
    private void doLogin(final String login, final String pass) {
        Thread t = new Thread() {
            public void run() {
                Looper.prepare();
                try {
                	Statusnet account = new Statusnet(login, pass);
                	progressDialog.dismiss();
                    Intent i = new Intent();
                    i.putExtra("userid", account.getName());
                    i.putExtra("password", pass);
                    mDbHelper.createAccount(login, pass, "http://identi.ca/api");
                    quit(true,i); 
                } catch (StatusnetException e) {
                    progressDialog.dismiss();
                    createDialog("Error", e.getMessage());
                }
                Looper.loop();                
            }
        };
        t.start();
    }
    
}
